package com.wsc.wscbirthdaykeeper.activities;

import java.sql.SQLException;
import java.util.Collections;

import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.wsc.wscbirthdaykeeper.R;
import com.wsc.wscbirthdaykeeper.activities.base.BaseActivity;
import com.wsc.wscbirthdaykeeper.adapter.PersonAdapter;
import com.wsc.wscbirthdaykeeper.managers.ResourceManager;
import com.wsc.wscbirthdaykeeper.model.entity.Person;
import com.wsc.wscbirthdaykeeper.persistence.dao.PersonDAO;
import com.wsc.wscbirthdaykeeper.utils.DialogUtils;
import com.wsc.wscbirthdaykeeper.utils.comparators.PersonComparator;

public class PersonListActivity extends BaseActivity {
	private ListView list;
	private PersonAdapter adapter;
	private boolean isChange;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_person_list);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onResume() {
		super.onResume();
		updatePersons();
		
		isChange = false;
	}

	public void initComponents() {
		list = (ListView) findViewById(R.id.persons);
	}

	public void initValues() {
		adapter = new PersonAdapter(PersonListActivity.this);
		list.setAdapter(adapter);
		registerForContextMenu(list);
	}

	private void updatePersons() {
		runtime.setPersons(null);
		if (runtime.getPersons() == null) {
			try {
				runtime.setPersons(PersonDAO.queryForAll());
			} catch (SQLException e) {
				showException(e);
			} catch (Exception e) {
				showException(e);
			}
		}

		Collections.sort(runtime.getPersons(), new PersonComparator());
		adapter.updatePersons(runtime.getPersons());
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_add:
			DialogUtils.personDialog(PersonListActivity.this, true, new Runnable() {

				@Override
				public void run() {
					isChange = true;
					updatePersons();
					list.invalidateViews();
				}
			});
			break;
		case R.id.menu_exit:
			finish();
			break;
		}
		return true;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		if (v.getId() == R.id.persons) {
			menu.setHeaderTitle(ResourceManager.getStringValue(R.string.action));
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(R.menu.menu_context, menu);
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
		Person person = runtime.getPersons().get(info.position);
		switch (item.getItemId()) {
		case R.id.context_menu_edit: {
			model.setPerson(person);
			DialogUtils.personDialog(PersonListActivity.this, false, new Runnable() {
				@Override
				public void run() {
					updatePersons();
					list.invalidateViews();
				}
			});
			break;
		}
		case R.id.context_menu_delete: {
			try {
				isChange = true;
				runtime.getPersons().remove(person);
				PersonDAO.delete(person);
				list.invalidateViews();
			} catch (java.sql.SQLException e) {
				showException(e);
			} catch (Exception e) {
				showException(e);
			}
			break;
		}
		}
		return true;
	}

	@Override
	protected void onPause() {
		super.onPause();

		if (isChange) {
			Intent intent = new Intent("android.appwidget.action.APPWIDGET_UPDATE");
			sendBroadcast(intent);
		}
	}

	@Override
	public void onBackPressed() {
		moveTaskToBack(true);
	}
}
